package com.droidblackbox.memory;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.droidblackbox.R;
import com.droidblackbox.manager.MemoryManager;
import com.droidblackbox.manager.SystemManager;
import com.droidblackbox.preference.PreferManager;

public class MemoryStatus extends Activity implements OnClickListener {

	private Button textUpButton = null;
	private Button textDownButton = null;

	private float textSize = 0.0F;

	private ProgressBar totalProgressBar = null;
	private ProgressBar usingAppProgressBar = null;

	private TextView usageExternalTextView = null;
	private TextView usageAppTextView = null;

	private TextView textExternalTotalTextView = null;
	private TextView textExternalAvailTextView = null;

	private TextView textAppTotalTextView = null;
	private TextView textAppAvailTextView = null;
	private TextView textAppUsageTextView = null;

	private long total;
	private long avail;
	private int usage;

	private long totalApp;
	private long availApp;
	private long usingApp;
	private int usageApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.memory_status);

		SystemManager.getInstance().initTitleBar(this);

		totalProgressBar = (ProgressBar) findViewById(R.id.totalProgressBar);
		usingAppProgressBar = (ProgressBar) findViewById(R.id.usingAppProgressBar);

		usageExternalTextView = (TextView) findViewById(R.id.usageExternalTextView);
		usageAppTextView = (TextView) findViewById(R.id.usageAppTextView);

		textExternalTotalTextView = (TextView) findViewById(R.id.textExternalTotalTextView);
		textExternalAvailTextView = (TextView) findViewById(R.id.textExternalAvailTextView);

		textAppTotalTextView = (TextView) findViewById(R.id.textAppTotalTextView);
		textAppAvailTextView = (TextView) findViewById(R.id.textAppAvailTextView);
		textAppUsageTextView = (TextView) findViewById(R.id.textAppUsageTextView);

		textSize = getResources().getDimension(R.dimen.log_text_size);
		textExternalTotalTextView.setTextSize(textSize);
		textExternalAvailTextView.setTextSize(textSize);

		textAppTotalTextView.setTextSize(textSize);
		textAppAvailTextView.setTextSize(textSize);
		textAppUsageTextView.setTextSize(textSize);

		textUpButton = (Button) findViewById(R.id.textUpButton);
		textDownButton = (Button) findViewById(R.id.textDownButton);

		textUpButton.setOnClickListener(this);
		textDownButton.setOnClickListener(this);

		memory();
	}

	private void memory() {
		StringBuffer sb = null;

		total = MemoryManager.getInstance().getTotalExternalMemorySize();
		avail = MemoryManager.getInstance().getAvailableExternalMemorySize();
		usage = MemoryManager.getInstance().getUsageExternalMemory();

		totalApp = Long.parseLong(PreferManager
				.get(PreferManager.KEY_LONG_MEMORY_SIZE));
		usingApp = MemoryManager.getInstance().getUsingAppMemorySize();

		availApp = totalApp - usingApp;
		usageApp = MemoryManager.getInstance().getPercent(usingApp, totalApp);

		sb = new StringBuffer();
		sb.append(getResources().getString(R.string.memory_external_usage));
		sb.append(" : ");
		sb.append(usage);
		sb.append("%");
		usageExternalTextView.setText(sb.toString());

		sb = new StringBuffer();
		sb.append(getResources().getString(R.string.memory_app_usage));
		sb.append(" : ");
		sb.append(usageApp);
		sb.append("%");
		usageAppTextView.setText(sb.toString());

		sb = new StringBuffer();
		sb.append(getResources().getString(R.string.memory_external_total_size));
		sb.append(" : ");
		sb.append(MemoryManager.getInstance().formatSize(total));
		textExternalTotalTextView.setText(sb.toString());

		sb = new StringBuffer();
		sb.append(getResources().getString(R.string.memory_external_avail_size));
		sb.append(" : ");
		sb.append(MemoryManager.getInstance().formatSize(avail));
		textExternalAvailTextView.setText(sb.toString());

		sb = new StringBuffer();
		sb.append(getResources().getString(R.string.memory_app_total_size));
		sb.append(" : ");
		sb.append(MemoryManager.getInstance().formatSize(totalApp));
		textAppTotalTextView.setText(sb.toString());

		sb = new StringBuffer();
		sb.append(getResources().getString(R.string.memory_app_avail_size));
		sb.append(" : ");
		sb.append(MemoryManager.getInstance().formatSize(availApp));
		textAppAvailTextView.setText(sb.toString());

		sb = new StringBuffer();
		sb.append(getResources().getString(R.string.memory_app_usage_size));
		sb.append(" : ");
		sb.append(MemoryManager.getInstance().formatSize(usingApp));
		textAppUsageTextView.setText(sb.toString());

		totalProgressBar.incrementProgressBy(usage);
		usingAppProgressBar.incrementProgressBy(usageApp);
	}

	public void onClick(View v) {
		if (v == textUpButton) {
			textSize++;
		} else if (v == textDownButton) {
			textSize--;
		} else {

		}

		textExternalTotalTextView.setTextSize(textSize);
		textExternalAvailTextView.setTextSize(textSize);

		textAppTotalTextView.setTextSize(textSize);
		textAppAvailTextView.setTextSize(textSize);
		textAppUsageTextView.setTextSize(textSize);

	}
}
